#include <QtGui/QMenuBar>
#include <QtGui/QStatusBar>
#include <QtGui/QApplication>
#include <QtGui/QToolBar>
#include <QtGui/QToolButton>
#include <QtCore/QSettings>
#include "Interface.h"
#include "Const.h"
#include "EditorGui.h"

EditorGui::EditorGui() {
	interface = new Interface(this);
	
	showStatusBarWidgetsButton = new QToolButton;
	showStatusBarWidgetsButton->setIcon(QIcon(":/icons/arrow-right.png"));
	showStatusBarWidgetsButton->setAutoRaise(true);
	showStatusBarWidgetsButton->setToolTip(tr("Show advanced editor settings"));
	showWidgetsInStatusBar = false;
	
	settingsStatusBarButton = new QToolButton;
	settingsStatusBarButton->setIcon(QIcon(":/icons/settings.png"));
	settingsStatusBarButton->setAutoRaise(true);
	settingsStatusBarButton->setPopupMode(QToolButton::InstantPopup);
	settingsStatusBarButton->setToolTip(tr("View settings"));
	
	statusBarMenu = new QMenu;
	
	showMenuBarAction = new QAction(this);
	showMenuBarAction->setText(tr("Show menubar"));
	showMenuBarAction->setCheckable(true);
	showMenuBarAction->setShortcut(QKeySequence("Ctrl+M"));
	
	showTabsAction = new QAction(this);
	showTabsAction->setText(tr("Show tabs bar"));
	showTabsAction->setCheckable(true);
	
	statusBarMenu->addAction(showMenuBarAction);
	statusBarMenu->addAction(interface->getToolBar()->toggleViewAction());
	statusBarMenu->addAction(showTabsAction);
	
	settingsStatusBarButton->setMenu(statusBarMenu);
	
	helpMenu = new QMenu("&" + tr("Help"));
	
	foreach (QAction *helpAction,interface->getHelpActions())
		helpMenu->addAction(helpAction);
	
	statusBar = new QStatusBar;
	statusBar->addWidget(showStatusBarWidgetsButton);
	foreach (QWidget *statusBarWidget,interface->getWidgetsForStatusBar()) {
		statusBarWidgets << statusBarWidget;
		statusBar->addWidget(statusBarWidget);
	}
	statusBar->addWidget(new QWidget,1);
	statusBar->addWidget(settingsStatusBarButton);
	statusBar->setMaximumHeight(25);
	statusBar->setStyleSheet("QStatusBar::item {border: 0px;}");
	
	setStatusBar(statusBar);
	
	// Create menus
	menuBar()->addMenu(interface->getFilesManagementMenu());
	menuBar()->addMenu(interface->getEditionMenu());
	menuBar()->addMenu(interface->getToolsMenu());
	menuBar()->addMenu(helpMenu);
	menuBar()->hide();
	
	foreach (QAction *invisibleAction,interface->getInvisibleActions())
		addAction(invisibleAction);
	
	addToolBar(Qt::LeftToolBarArea,interface->getToolBar());
	
	connect(interface,SIGNAL(quit()),this,SLOT(quit()));
	connect(interface,SIGNAL(changeWindowTitle(const QString&)),this,SLOT(changeWindowTitle(const QString&)));
	connect(showMenuBarAction,SIGNAL(toggled(bool)),menuBar(),SLOT(setVisible(bool)));
	connect(showTabsAction,SIGNAL(toggled(bool)),interface,SLOT(setTabsVisible(bool)));
	connect(showStatusBarWidgetsButton,SIGNAL(clicked()),this,SLOT(hideOrShowWidgetsInStatusBar()));
	
	setCentralWidget(interface->getAdvancedEditorView());
	
	setWindowIcon(QIcon(":/icons/main.png"));
	setWindowTitle("Vialinx Editor");
	resize(800,600);
	
	loadSettings();
}

EditorGui::~EditorGui() {
	delete interface;
	delete helpMenu;
	delete statusBarMenu;
	delete showStatusBarWidgetsButton;
	delete settingsStatusBarButton;
}

void EditorGui::changeWindowTitle(const QString& title) {
	setWindowTitle(title.isEmpty() ? "Vialinx Editor" : title + " - Vialinx Editor");
}

void EditorGui::saveSettings() {
	QSettings settings(ORGANIZATION,PROGRAM_NAME);
	settings.setValue("MainWindow/Position",rect().topLeft());
	settings.setValue("MainWindow/Size",size());
	settings.setValue("MainWindow/ShowMenuBar",showMenuBarAction->isChecked());
	settings.setValue("MainWindow/ShowFileToolBar",!interface->getToolBar()->isHidden());
	settings.setValue("MainWindow/ShowTabBar",showTabsAction->isChecked());
	settings.setValue("MainWindow/State",saveState());
	settings.setValue("MainWindow/ShowWidgetsInStatusBar",showWidgetsInStatusBar);
}

void EditorGui::loadSettings() {
	QSettings settings(ORGANIZATION,PROGRAM_NAME);
	move(settings.value("MainWindow/Position",QPoint(50,50)).toPoint());
	resize(settings.value("MainWindow/Size",QSize(800,600)).toSize());
	showMenuBarAction->setChecked(settings.value("MainWindow/ShowMenuBar",true).toBool());
	interface->getToolBar()->setVisible(settings.value("MainWindow/ShowFileToolBar",false).toBool());
	showTabsAction->setChecked(settings.value("MainWindow/ShowTabBar",false).toBool());
	restoreState(settings.value("MainWindow/State").toByteArray());
	showWidgetsInStatusBar = !settings.value("MainWindow/ShowWidgetsInStatusBar",true).toBool();
	hideOrShowWidgetsInStatusBar();
}

void EditorGui::closeEvent(QCloseEvent *) {
	quit();
}

void EditorGui::quit() {
	saveSettings();
	qApp->quit();
}

void EditorGui::openFiles(QStringList filesPathes) {
	interface->openFiles(filesPathes);
}

void EditorGui::hideOrShowWidgetsInStatusBar() {
	showWidgetsInStatusBar = !showWidgetsInStatusBar;
	if (showWidgetsInStatusBar)
		showStatusBarWidgetsButton->setIcon(QIcon(":/icons/arrow-left.png"));
	else
		showStatusBarWidgetsButton->setIcon(QIcon(":/icons/arrow-right.png"));
	foreach (QWidget *widget,statusBarWidgets)
		widget->setVisible(showWidgetsInStatusBar);
}
